$(function() {

    //IE层级兼容问题
    $(".drop-box").each(function(i) {
        $(this).css('z-index', $(".drop-box").length - i);
    });
    // 装点
    var areaChangeFrom_zd = new areaSelectChange("areaSelectFrom-zd", {
        hasHot: false,
        hideName: {
            provinceName: "startProvinceId",
            cityName: "startCityId",
            countyName: "startDistrictId"
        }
    });
    // 卸点
    var areaChangeFrom_xd = new areaSelectChange("areaSelectFrom-xd", {
        hasHot: false,
        hideName: {
            provinceName: "endProvinceId",
            cityName: "endCityId",
            countyName: "endDistrictId"
        }
    });

    // 货物
    $(".goodstxt").click(function() {
        var $this = $(this);
        var $arrow = $(this).find("span");
        var $box = $this.next("#goodsdown");
        var sid = $this.data('sid');
        var ulhtml = '<ul>';

        $.each(breedArray, function(index, item) {
            //拼ul结构之后插入city内容
            ulhtml += '<li data-id="' + item.n.i + '" data-sid="' + index + '">' + item.n.n + '</li>';
        });
        ulhtml += '</ul>';

        $("#goodsdown").html(ulhtml);
        if ($arrow.hasClass("arrow")) {

            $arrow.removeClass("arrow").addClass("open");
            $("#goodsdown").show();
        } else {
            $arrow.removeClass("open").addClass("arrow");
            $("#goodsdown").hide();
        }

    });
    $("#goodsdown").on("click", "li", function() {
        var $cont = $(this).text();
        var id = $(this).data('id');
        $(".goods_title").text($cont);

        //hidden值
        $("#breed").val($cont);
        $("#breedId").val(id);

        $(".goodstxt span").removeClass("open").addClass("arrow");
        $("#goodsdown").hide();

    });

    //点击任何地方关闭下拉框
    $(document).on('click', function(e) {
        var $this = $(e.target);
        var $item = $('#wlbanform .goods-box');
        if (!$this.hasClass('goods-box') && !$item.has($this).length) {
            $item.children('.goodsdown').css('display', 'none');
            $(".goodstxt").children('span').removeClass('open').addClass('arrow');
        }
    });


    function getMyCookie(key) {
        var val = "";

        // 对cookie操作
        var cookies = document.cookie;
        cookies = cookies.replace(/\s/, "");
        var cookie_array = cookies.split(";");
        for (i = 0; i < cookie_array.length; i++) {
            // key=lilei
            var cookie = cookie_array[i];
            var array = cookie.split("=");
            if (array[0].trim() == key.trim()) {
                val = array[1];
            }
        }
        return val;
    }
	function getCookie(name) {
	  const arr = document.cookie.match(new RegExp(`(^| )${name}=([^;]*)(;|$)`));
	  if (arr != null) {
		const data=unescape(arr[2])
	    return data?data.replace(/\"/g, ''):"";
	  }
	  return null;
	  // Cookies.get(name);
	}
    //快速找车
    $(".btn-send").click(function() {
        var userInfo = getMyCookie("userInfo");
        
        if (userInfo == '' || userInfo == null || userInfo == undefined) {
            $("#dialog3").show();
            $("#dialog-face").show();
            return;
        }
        var load = "";
        var params = {}
        if (areaChangeFrom_zd.currentArea.p != null && areaChangeFrom_zd.currentArea.p != "") {
            // load = areaChangeFrom_zd.currentArea.p;
            params.loadProvince = areaChangeFrom_zd.currentArea.p
        }
        if (areaChangeFrom_zd.currentArea.c != null && areaChangeFrom_zd.currentArea.c != "") {
            // load += ","+areaChangeFrom_zd.currentArea.c;
            params.loadCity = areaChangeFrom_zd.currentArea.c
        }
        if (areaChangeFrom_zd.currentArea.t != null && areaChangeFrom_zd.currentArea.t != "") {
            // load += ","+ areaChangeFrom_zd.currentArea.t;
            params.loadDistricts = areaChangeFrom_zd.currentArea.t
        }
        var unload = "";
        if (areaChangeFrom_xd.currentArea.p != null && areaChangeFrom_xd.currentArea.p != "") {
            params.unloadProvince = areaChangeFrom_xd.currentArea.p;
        }
        if (areaChangeFrom_xd.currentArea.c != null && areaChangeFrom_xd.currentArea.c != "") {
            params.unloadCity = areaChangeFrom_xd.currentArea.c;
        }
        if (areaChangeFrom_xd.currentArea.t != null && areaChangeFrom_xd.currentArea.t != "") {
            params.unloadDistricts = areaChangeFrom_xd.currentArea.t;
        }
        params.breed = $("#breed").val();
        params.weight = $("#weight").val();
        // var breed = $("#breed").val();
        // var weight = $("#weight").val();
        // $(".mask").show();
        // dialog({
        // 	title: '提示',
        // 	content: '<div class="bounced">此线路的具体信息，请联系及韵物流工作人员:021-66896543</div>',
        // 	width:'420',
        // 	height:'50',
        // 	fixed:true,
        // 	okValue: '确定',
        // 	ok: function () {
        // 		$(".mask").hide();
        // 	},
        // 	cancel: false
        // }).show();
        // window.location.href="http://www.56jiyun.com/jywl/publishDemand.htm"
        $.ajax({
			headers:{JywlMemberBanksteelToken:getCookie('JYWL_MEMBER_BANKSTEEL_SESSION'),serverfrom: 5},
            type: "post",
            // url: "http://www.56jiyun.com/jywl/publishDemand.htm?load=" +load+ "&unload=" +unload + "&breed=" + breed + "&weight=" + weight,
            url: "//www.56jiyun.com/customerapp/jywl/customer/api/will/saveWill/v1",
            data: params,
            success: function(date) {
                if (date.code == "200") {
                    alert(date.message);
                    // window.location.reload();
                } else {
                    alert(date.message);
                }
            }
        });

    });

    jQuery(".line_recommend").slide({ mainCell: ".bd ul", effect: "leftLoop", vis: 4, autoPlay: false }); //优质路线推荐

    //查询需求单
    // xqdFun();
    $("#xqd_tab li").click(function() {
        var index = $(this).index();
        if (index == 0) {
            $(this).addClass("on");
            $(this).siblings().removeClass("on");
        }
        // xqdFun(index);
    })
});

function xqdFun(index) {
    var url = "//www.56jiyun.com/jywl/customer/demand/listOfAllDemand.htm";
    if (index == 1) {
        url = "//www.56jiyun.com/jywl/customer/demand/listOfReceiptedDemand.htm";
    }
    $.ajax({
        type: "GET",
        url: url,
        success: function(data) {
            if (data) {
                if (data.code == "200") {
                    if (index == 1) {
                        $("#xqd_tab li").eq(1).addClass("on");
                        $("#xqd_tab li").eq(0).removeClass("on");
                    }
                    var records = data.data,
                        html = "";
                    $(".line-xqd .bd").html('<span class="hd"><i class="prev"><b></b></i><i class="next"><b></b></i></span><ul class="conten" id="xqd_content"></ul>');
                    for (var i = 0; i < records.length; i++) {
                        html += "<li>" +
                            '<div class="inner">' +
                            '<div class="title">发布日期：<span>' + records[i].time + '</span></div>' +
                            '<div class="desc"><span class="sp3">装点：</span><span class="sp1" title="' + records[i].loadAddress + '">' + records[i].loadAddress + '</span></div>' +
                            '<div class="desc"><span class="sp3">卸点：</span><span class="sp1" title="' + records[i].unloadAddress + '">' + records[i].unloadAddress + '</span></div>' +
                            '<div class="desc"><span class="sp3">物资：</span><span class="sp2" title="' + records[i].itemInfo + '">' + records[i].itemInfo + '</span></div>' +
                            '<div class="desc"><span class="sp3">联系人：</span><span class="sp2">' + records[i].contactor + '</span></div>' +
                            '<div class="desc"><span class="sp3">联系方式：</span><span class="sp2">' + records[i].contactTel + '</span></div>' +
                            (index == 1 ? '<a class="order1" href="javascript:;">已接单</a>' : ('<a class="order" href="javascript:orderFun(' + records[i].demandId + ');">接单</a>')) +
                            '</div>' +
                            "</li>";
                    }
                    $(".line-xqd .bd").find("#xqd_content").append(html);
                    jQuery(".line-xqd").slide({ mainCell: ".bd ul", effect: "leftLoop", vis: 4, autoPlay: false }); //需求单
                } else if (data.code == "302") {
                    loginDialog = dialog({
                        id: 'login_tip',
                        title: '提示',
                        content: '<div style="text-align:center;line-height:50px;font-size:14px;">请先登录！</div>',
                        width: 250,
                        okValue: '确定',
                        ok: function() {
                            window.location.href = "//www.56jiyun.com/jywl/login.htm/?mybackurl=//www.56jiyun.com"
                        },
                        cancelValue: '取消',
                        cancel: function() {},
                        fixed: true
                    });
                    Autofixed('#login_tip', loginDialog);
                    loginDialog.showModal();
                } else {
                    orderDialog1 = dialog({
                        id: 'order_tip1',
                        title: '提示',
                        content: '<div style="text-align:center;line-height:50px;font-size:14px;">' + data.description + '</div>',
                        width: 250,
                        okValue: '关闭',
                        ok: function() {

                        },
                        fixed: true
                    });
                    Autofixed('#order_tip1', orderDialog1);
                    orderDialog1.showModal();
                }
            }
        }
    });
}

function orderFun(demandId) {
    $.ajax({
        type: "POST",
        url: "//www.56jiyun.com/jywl/customer/demand/receiptDemand.htm",
        data: { demandId: demandId },
        success: function(data) {
            if (data) {
                if (data.code == "200") {
                    //xqdFun();
                    orderDialog = dialog({
                        id: 'order_tip',
                        title: '提示',
                        content: '<div style="text-align:center;line-height:50px;font-size:14px;">接单成功！</div>',
                        width: 250,
                        okValue: '关闭',
                        ok: function() {

                        },
                        fixed: true
                    });
                    Autofixed('#order_tip', orderDialog);
                    orderDialog.showModal();
                } else if (data.code == "302") {
                    loginDialog = dialog({
                        id: 'login_tip',
                        title: '提示',
                        content: '<div style="text-align:center;line-height:50px;font-size:14px;">请先登录！</div>',
                        width: 250,
                        okValue: '确定',
                        ok: function() {
                            window.location.href = "//www.56jiyun.com/jywl/login.htm/?mybackurl=//www.56jiyun.com"
                        },
                        cancelValue: '取消',
                        cancel: function() {},
                        fixed: true
                    });
                    Autofixed('#login_tip', loginDialog);
                    loginDialog.showModal();
                } else {
                    orderDialog = dialog({
                        id: 'order_tip',
                        title: '提示',
                        content: '<div style="text-align:center;line-height:50px;font-size:14px;">' + data.description + '</div>',
                        width: 250,
                        okValue: '关闭',
                        ok: function() {

                        },
                        fixed: true
                    });
                    Autofixed('#order_tip', orderDialog);
                    orderDialog.showModal();
                }
            }
        }
    });
}

function formatSeconds(value) { //毫秒数转化为年月日时分秒格式
    var date = new Date(value);
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    var Hour = date.getHours();
    var Minute = date.getMinutes();
    var Second = date.getSeconds();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if (Hour >= 0 && Hour <= 9) {
        Hour = "0" + Hour;
    }
    if (Minute >= 0 && Minute <= 9) {
        Minute = "0" + Minute;
    }
    if (Second >= 0 && Second <= 9) {
        Second = "0" + Second;
    }
    var dateTime = month + seperator1 + strDate +
        " " + Hour + seperator2 + Minute;
    return dateTime;
}